$(function(){
    $(".squares").attr("style","display:none")
    $(".about").attr("style","display:none")
    $(".trace").attr("style","display:none")
    //初始分页查询页数
    function pageSize(user) {
        var size;
        var url;
        if( user == null){
            url = "/blog/article?key=pageSize";
        } else {
            url = "/blog/article?key=pageSize&user=T";
        }
        $.get(url,"",function (data) {
            alert(data);
        })
        return size;
    }
    pageSize();
    //删除文章函数
    function deleteArticle(){
        $("#bolgcontent button").each(function () {
            $(this).unbind("click");})
        $("#bolgcontent button").each(function () {
            $(this).on("click",function () {
                var ok = confirm("是否删除");
                if(ok){
                    $.post("/blog/server/user","key=deleteArticle&id="+$(this).attr("id"),function (data) {
                        if(data=="T"){
                            alert("删除成功,电波消失");
                            flushArticle();
                        } else if(data=="F"){
                            alert("未知电波错误!");
                        }
                    },"Text");
                }else {
                    alert("取消");
                }
            })
        })
    }
    //接收文章函数
    function  flushArticle() {
        $.post("/blog/server/user","key=getUserArticle",function (data) {
            $("#bolgcontent").html(data);
            deleteArticle();
        },"Text");
    }
    //接收广场函数
    function  squaresflushArticle() {
        $.post("/blog/server/user","key=getAllArticle&usr=all",function (data) {
            $("#squarescontent").html(data);
        },"Text");
    }

    //验证登录
    $.post("/blog/server/user","key=getUserInfo",function (data) {
        if(data == "F"){
            alert("请先登录");
            window.location.href = "../login/login.html";
        }else{
            $("#userName").html("用户:"+data);
            flushArticle();
        }
    },"Text");

    //--------------------------------------------------------------心情日记
    //发送按钮
    $("#article").on("click",function () {
        var caption = $("#caption").val().replaceAll(/\s/g,"");
        var artcontent = $("#artcontent").val();
        if(caption.length == 0 || artcontent ==0){
          alert("标题/内容不能为空，电波会发送失败的……");
        }else{
            var date = new Date();
            var time = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
            //为T表示私密
            var publicd = $(":input:checkbox:checked").val();
            if(publicd==undefined) {
                publicd = "F";
            }
            $.post("/blog/server/user","key=sendArticle&caption="+caption+"&artcontent="+artcontent+"&time="+time+"&publicd="+publicd,function (data) {
                if(data=="T"){
                    //发送成功后清除数据
                    $("#caption").val("");
                    $("#artcontent").val("");
                    $("#publicd").attr("checked",false);
                    alert("心情电波发送成功!");
                    //发送成功刷新文章
                    flushArticle();
                } else if(data=="F"){
                    alert("遇到未知电子错误!")
                }
            },"Text")
        }
    })
    //取消按钮
    $("#removeArticle").on("click",function () {
        $("#caption").val("");
        $("#artcontent").val("");
        $("#publicd").attr("checked",false);
    })
    //分页查询
    //导航栏
    //心情按钮
    $("#xq").on("click",function () {
        //点击心情按钮后刷新文章
        flushArticle();
        $(".log").attr("style","display:block;display:flex;")
        $(".squares").attr("style","display:none")
        $(".about").attr("style","display:none")
        $(".trace").attr("style","display:none")
    })

    //广场按钮
    $("#gc").on("click",function () {
        $(".log").attr("style","display:none")
        $(".squares").attr("style","display:block;display:flex;")
        squaresflushArticle();
        $(".about").attr("style","display:none")
        $(".trace").attr("style","display:none")
    })

    $("#gy").on("click",function () {
        $(".log").attr("style","display:none")
        $(".squares").attr("style","display:none")
        $(".about").attr("style","display:block;display:flex;")
        $(".trace").attr("style","display:none")
    })

    $("#zj").on("click",function () {
        $(".log").attr("style","display:none")
        $(".squares").attr("style","display:none")
        $(".about").attr("style","display:none;")
        $(".trace").attr("style","display:block;display:flex;");
    })
})